//
//  HMComposeToolBar.swift
//  微博
//
//  Created by 程双岐 on 16/9/28.
//  Copyright © 2016年 heima. All rights reserved.
//

import UIKit

enum HMComposeToolBarButtonType: Int {
    case picture = 0
    case mention = 1
    case trend = 2
    case emoticon = 3
    case add = 4
}

@available(iOS 9.0, *)
class HMComposeToolBar: UIStackView {

    var callBack: ((HMComposeToolBarButtonType)->())?
    var emoticonButton: UIButton = UIButton()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        axis = .horizontal
        distribution = .fillEqually
        backgroundColor = RandomColor()
        addcChileButton(imageName: "compose_toolbar_picture", type: .picture)
        addcChileButton(imageName: "compose_mentionbutton_background", type: .mention)
        addcChileButton(imageName: "compose_trendbutton_background", type: .trend)
       emoticonButton = addcChileButton(imageName: "compose_emoticonbutton_background", type: .emoticon)
        addcChileButton(imageName: "compose_add_background", type: .add)
    }
    //可以忽略返回值的处理
    @discardableResult
    private func addcChileButton(imageName: String,type: HMComposeToolBarButtonType) -> UIButton{
        let button = UIButton()
        button.tag = type.rawValue
        button.addTarget(self, action: #selector(btnAction(btn:)), for: .touchUpInside)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), for: .highlighted)
        button.setImage(UIImage(named: "\(imageName)"), for: .normal)
        button.adjustsImageWhenHighlighted = false
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), for: .normal)
        addArrangedSubview(button)
        return button
    }
    
    @objc private func btnAction(btn: UIButton){
        
        let type = HMComposeToolBarButtonType(rawValue: btn.tag)!
        
        callBack?(type)
    }
    
    //根据表情键盘类型显示不同的icon
    func showIcon(isEmoticon: Bool) {
        if isEmoticon {
            emoticonButton.setImage(UIImage(named: "compose_keyboardbutton_background"), for: .normal)
            emoticonButton.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), for: .highlighted)
        }else{
            emoticonButton.setImage(UIImage(named: "compose_emoticonbutton_background"), for: .normal)
            emoticonButton.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), for: .highlighted)
            
        }
    }
}
